package com.example.appkcsj.slice;

import com.example.appkcsj.ResourceTable;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

public class fifthAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Text t1,t2;
    Button btu5;
    private DBUtils dbUtils;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fifth);
        t1= (TextField) findComponentById(ResourceTable.Id_field10);
        t2= (TextField) findComponentById(ResourceTable.Id_field11);
        btu5= (Button) findComponentById(ResourceTable.Id_but5);
        //给按钮添加点击事件
        if(intent!=null){
            String name=intent.getStringParam("t1");
            String pass1=intent.getStringParam("t2");
            /**String pass2=intent.getStringParam("password2");
            Student student=new Student(name,pass1,pass2);
            dbUtils.insertStudent(student);*/
        }
        btu5.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if ("".equals(t1.getText()) || "".equals(t2.getText())) {
                    ToastDialog td = new ToastDialog(fifthAbilitySlice.this);
                    td.setText("请输入用户名及密码！");
                    td.show();
                } else {
                    if("123".equals(t1.getText()) || "123".equals(t2.getText())) {
                        if ("123".equals(t1.getText()) && "123".equals(t2.getText())) {
                            Intent m = new Intent();
                            m.setParam("t1","123");
                            m.setParam("t2","123");
                            present(new fifthAbilitySlice(),m);
                            Operation operation = new Intent.OperationBuilder()
                                    .withDeviceId("")
                                    .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                    .withAbilityName("com.example.appkcsj.thirdAbility")//要跳转的包名
                                    .build();//将上面的信息打包
                            m.setOperation(operation);
                            startAbility(m);
                            ToastDialog td = new ToastDialog(fifthAbilitySlice.this);
                            td.setText("登录成功！");
                            td.show();
                        }
                        else{
                            ToastDialog td = new ToastDialog(fifthAbilitySlice.this);
                            td.setText("密码输入错误！");
                            td.show();
                        }
                    }
                    else{
                        if ("".equals(t2.getText())) {
                            ToastDialog td = new ToastDialog(fifthAbilitySlice.this);
                            td.setText("用户名输入错误！");
                            td.show();
                        } else{
                            ToastDialog td = new ToastDialog(fifthAbilitySlice.this);
                            td.setText("用户名及密码输入错误!");
                            td.show();
                        }
                    }
                }

            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {

    }
}
